package com.tanhua.server.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.RecommendUserApi;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.vo.TodayBest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 推荐用户业务层
 * 负责通过dubbo与RecommendUserApi【推荐用户api】交互
 */
@Service
public class RecommendUserService {
    @Reference(version = "1.0.0")
    private RecommendUserApi recommendUserApi;


    /**
     * 查询今日佳人数据
     *
     * @param userId 用户id
     * @return
     */
    public TodayBest queryWithMaxScore(Long userId) {
        // 根据根据用户id查询推荐用户id【今日佳人】
        RecommendUser recommendUser = this.recommendUserApi.queryWithMaxScore(userId);
        if (recommendUser == null) {
            // 没有查询出佳人信息
            return null;
        }

        // 创建TodayBest佳人对象
        TodayBest todayBest = new TodayBest();

        // 封装数据
        todayBest.setId(recommendUser.getUserId()); // 佳人id

        Double fate = Math.floor(recommendUser.getScore());
        todayBest.setFateValue(fate.longValue()); // 缘分值

        return todayBest;
    }


    /**
     * 查询推荐朋友
     *
     * @param userId   用户id
     * @param pageNum  当前页
     * @param pageSize 每页条数
     * @return
     */
    public PageInfo<RecommendUser> queryRecommendUserList(Long userId, Integer pageNum, Integer pageSize) {
        PageInfo<RecommendUser> recommendUserPageInfo = this.recommendUserApi.queryPageInfo(userId, pageNum, pageSize);

        if (recommendUserPageInfo != null) {
            return recommendUserPageInfo;
        }

        return null;
    }

    /**
     * 查询推荐好友的缘分值
     *
     * @param userId   好友的id
     * @param toUserId 我的id
     * @return
     */
    public Long queryScore(Long userId, Long toUserId) {
        Double score = this.recommendUserApi.queryScore(userId, toUserId);
        if (ObjectUtil.isEmpty(score)) {
            // 没有缘分值
            score = -11D;
        }
        // 缘分值向下取整并转换成Long类型
        Long floorScore = Convert.toLong(Math.floor(score));
        return floorScore;
    }

    /**
     * 查询卡片列表
     *
     * @param userId 用户id
     * @param count  查询个数
     * @return
     */
    public List<RecommendUser> queryCardList(Long userId, int count) {
        return this.recommendUserApi.queryCardList(userId, count);
    }
}
